/*
#include <reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
	
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

uint ADresult[11];

sbit DATOUT = P1^0;   
sbit DATIN  = P1^1;   
sbit CS     = P1^2;    
sbit IOCLK   = P1^3; 
sbit EOC    = P3^4; 
sbit bit1   = P1^4;
sbit bit2   = P1^5;
sbit bit3   = P1^6;
sbit bit4   = P1^7;

void delay(uint i) {
	while(i--);
}

uint getdata(uchar channel) {  
   uchar i,temp;     
   uint read_ad_data=0; 
   channel=channel<<4;                           
   IOCLK=0;
   CS=0;    
   temp=channel;  
 
   for(i=0;i<12;i++) {  
		if(DATOUT) read_ad_data=read_ad_data|0x01; 
		DATIN=(bit)(temp&0x80);
		IOCLK=1;   
		_nop_();_nop_();_nop_();
		IOCLK=0; 
		_nop_();_nop_();_nop_();
		temp=temp<<1;
		read_ad_data<<=1; 
  }
   
  CS=1;    
  read_ad_data>>=1;  
  return(read_ad_data);
}

void dispaly(void) {
	uchar qian,bai,shi,ge; 
	uint value;
	value=ADresult[1]*1.221;//*5000/4095
	qian=value%10000/1000;
	bai=value%1000/100;
	shi=value%100/10;
	ge=value%10;

	bit1=0;
	P0=table[qian]-128; //1000 0000
	delay(1000);
	bit1=1;

	bit2=0;
	P0=table[bai];
	delay(1000);
	bit2=1;

	bit3=0;
	P0=table[shi];
	delay(1000);
	bit3=1;
	bit4=0;
	P0=table[ge];
	delay(1000);
	bit4=1;
}

void main() { 
	ADresult[1]=getdata(1);
	
	while(1){
		_nop_();_nop_();_nop_();
		ADresult[1]=getdata(1); 
		while(!EOC);  
		dispaly();
	}
}
*/

#include <reg51.h>
#include<intrins.h>

#define uchar unsigned char
#define uint unsigned int
#define LCD_PORT P0

sbit DATOUT = P1^0;   
sbit DATIN  = P1^1;   
sbit CS     = P1^2;    
sbit IOCLK  = P1^3; 
sbit EOC    = P3^4; 
sbit bit1   = P1^4;
sbit bit2   = P1^5;
sbit bit3   = P1^6;
sbit bit4   = P1^7;
sbit RS     = P2^0;
sbit RW     = P2^1;
sbit EN     = P2^2;

uint ADresult[11];

uint getdata(uchar channel) {  
   uchar i,temp;     
   uint read_ad_data=0; 
   channel=channel<<4;                           
   IOCLK=0;
   CS=0;    
   temp=channel;  
 
   for(i=0;i<12;i++) {  
		if(DATOUT) read_ad_data=read_ad_data|0x01; 
		DATIN=(bit)(temp&0x80);
		IOCLK=1;   
		_nop_();_nop_();_nop_();
		IOCLK=0; 
		_nop_();_nop_();_nop_();
		temp=temp<<1;
		read_ad_data<<=1; 
  }
   
  CS=1;    
  read_ad_data>>=1;  
  return(read_ad_data);
}

// simple delay
void delay_ms(unsigned int ms)
{
    unsigned int i,j;
    for(i=0; i<ms; i++)
        for(j=0; j<120; j++);
}

// enable pulse
void lcd_enable()
{
    EN = 1;
    delay_ms(1);
    EN = 0;
    delay_ms(1);
}

// write command
void lcd_write_cmd(unsigned char cmd)
{
    RS = 0;        // command
    RW = 0;        // write
    LCD_PORT = cmd;
    lcd_enable();
}

// write data
void lcd_write_data(unsigned char dat)
{
    RS = 1;        // data
    RW = 0;        // write
    LCD_PORT = dat;
    lcd_enable();
}

// init LCD1602
void lcd_init()
{
    delay_ms(20);       // wait for LCD power on
    lcd_write_cmd(0x38); // 8-bit, 2 lines
    delay_ms(5);
    lcd_write_cmd(0x0C); // display on, cursor off
    delay_ms(5);
    lcd_write_cmd(0x06); // entry mode
    delay_ms(5);
    lcd_write_cmd(0x01); // clear display
    delay_ms(5);
}

// set cursor
void lcd_set_cursor(unsigned char row, unsigned char col)
{
    unsigned char addr;
    if(row == 0) addr = 0x80 + col;   // first row
    else         addr = 0xC0 + col;   // second row
    lcd_write_cmd(addr);
}

// print float in X.XXX format
void lcd_print_voltage(uint ADresult)
{
    unsigned int frac;
    unsigned char buf[6];
	
	uchar qian,bai,shi,ge; 
	uint value;
	value=ADresult*1.221;//*5000/4095
	qian=value%10000/1000;
	bai=value%1000/100;
	shi=value%100/10;
	ge=value%10;

    // integer part
    buf[0] = '0';
    buf[1] = '.';
    buf[2] = bai + '0';
    buf[3] = shi + '0';
    buf[4] = ge  + '0';
    buf[5] = '\0';

    // print label
    lcd_set_cursor(0, 0);
    lcd_write_data('V');
    lcd_write_data('o');
    lcd_write_data('l');
    lcd_write_data('t');
    lcd_write_data('a');
    lcd_write_data('g');
    lcd_write_data('e');
    lcd_write_data(':');
    lcd_write_data(' ');

    // print voltage X.XXX
    for(frac = 0; buf[frac] != '\0'; frac++)
        lcd_write_data(buf[frac]);
}

void main()
{
	ADresult[1]=getdata(1);
	lcd_init();
	while(1){
		_nop_();_nop_();_nop_();
		ADresult[1]=getdata(1); 

		while(!EOC);  
		lcd_print_voltage(ADresult[1]);
	}

}
